package com.company.project.bean;

import java.lang.reflect.Field;

/**
 * IntelliJ IDEA.
 * User: lwb
 * Date: 2017/11/23
 * Time: 8:43
 * function:
 */
public class ErrorParam {

    private String msg ;
    private Integer id;
    private String action;

    @Override
    public String toString() {

        Class clazz = this.getClass();
        Object obj = this;
        String params = packageUrlParams(clazz, obj);
        return params;
    }

    /**
     * 将对象的字段成url的请求参数如：?name=xiaoming&id=8&
     * @param clazz
     * @param obj
     * @return
     */
    private String packageUrlParams(Class clazz, Object obj) {
        Field[] fields = clazz.getDeclaredFields();
        StringBuilder bd = new StringBuilder();
        bd.append("?");
        for(Field f : fields)
        {
            String name = f.getName();
            Object value = null;
            try {
                value = f.get(obj);
                if(value == null)
                {
                    Class fieldType = f.getType();
                    if(fieldType == Integer.class)
                    {
                        value = 0;
                    }
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            bd.append(name).append("=").append(value).append("&");
        }
        return bd.toString();
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }
}
